package com.stu.Demo;

import com.stu.entity.Dept;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

/**
 * @author zy
 * @date 2025/3/3 14:34
 */
public class JDBCDemo2 {
    public static void main(String[] args) throws Exception {
        // 加载数据库驱动类(Java 连接DataBase 使用到的类与接口)
        Class.forName("com.mysql.cj.jdbc.Driver");

        //连接数据库
        Connection conn = null;
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/empdb?useSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai",
                "root", "root");

        // SQL语句
        Dept dept = new Dept(70,"策划部","上海");
        PreparedStatement pstmt = null;
        pstmt = conn.prepareStatement("insert into dept values(?,?,?)");
        pstmt.setInt(1, dept.getDeptno());
        pstmt.setString(2, dept.getDname());
        pstmt.setString(3, dept.getLoc());

        pstmt.executeUpdate();// 返回修改行数

        // 释放资源
        if(pstmt != null) {
            pstmt.close();
        }
        if(conn != null) {
            conn.close();
        }
    }
}
